package io.adrainty.bolee.framework.gateway.configuration;

import cn.hutool.core.lang.Snowflake;
import io.adrainty.bolee.framework.commons.properties.SecurityConfigProperties;
import io.adrainty.bolee.framework.gateway.exception.GatewayGlobalExceptionHandler;
import io.adrainty.bolee.framework.gateway.properties.LogRecordProperties;
import io.adrainty.bolee.framework.commons.properties.SnowflakeProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

/**
 * @author AdRainty
 * @version V1.0.0
 * @since 2025/7/27 下午10:47
 */

@Configuration
@EnableConfigurationProperties({
        LogRecordProperties.class,
        SnowflakeProperties.class,
        SecurityConfigProperties.class
})
@SuppressWarnings("all")
public class GatewayConfiguration {

    @Bean(name = "gatewaySnowflake")
    public Snowflake snowflake(@Autowired SnowflakeProperties properties) {
        return new Snowflake(properties.getWorkerId(), properties.getDatacenterId());
    }

    @Bean
    @Primary
    @Order(Ordered.HIGHEST_PRECEDENCE)
    public ErrorWebExceptionHandler errorWebExceptionHandler() {
        return new GatewayGlobalExceptionHandler();
    }

}
